<?php
session_start();
if (!isset($_SESSION['admin']))
    header('location:index.php');
?>

<!DOCTYPE HTML>
<html>

    <head>
        <title>3D|didongde 3D- Di động đây</title>
        <?php include('modules/page_head.php'); ?>
        <style type="text/css">
            form input {
                width:400px;
            }
        </style>
    </head>

    <body>
        <div id="main">
            <div id="header">
                <div id="logo">
                    <h1><a href="index.php">Didongde</a></h1>
                    <div class="slogan">Where you can find your own best suitable mobile</div>
                    <?php include('modules/logo_header.php'); ?>
                </div>
                <div id="menubar">
                    <?php include('modules/menubar.php'); ?>
                </div>
            </div>
            <div id="site_content">
                <div id="sidebar_container">
                    <h2>Tìm kiếm nhanh</h2>
                    <div class="sidebar">
                        <?php
                        include ("modules/quick_search.php");
                        show_manu();
                        ?>
                    </div>
                    <div class="sidebar">
                        <?php show_price(); ?>
                    </div>
                </div>
                <div id="content">
                    <!-- insert the page content here -->
                    <h2>Sửa thông tin của điện thoại</h2>
                    <?php
                    include_once('conf/dbconfig.php');
                    $cfg = new dbconfig();
                    $host = $cfg->get_dbhost();
                    $db = $cfg->get_dbname();
                    $usr = $cfg->get_dbusername();
                    $pass = $cfg->get_dbpwd();
                    $connection = pg_connect("host = $host dbname = $db user = $usr password = $pass")
                            or die("Cannot connect to database server: ");
                    ?>
                    <div>
                        <form method="get" action="#">
                            <table>
                                <tr><td>Model</td><td>
                                        <?php
                                        $query = "select * from mobilephone order by model";
                                        $result = pg_query($connection, $query);
                                        echo "<select id=\"model\" name = \"model\">";
                                        echo "<option value=\"này\">Chọn mẫu điện thoại</option>";
                                        for ($i = 0; $i < pg_num_rows($result); $i++) {
                                            $name = pg_fetch_result($result, $i, 'model');
                                            echo "<option value = \"$name\"";
                                            if (isset($_REQUEST['model']) && $_REQUEST['model'] == $name) {
                                                echo " selected=\"selected\"";
                                                $mbid = pg_fetch_result($result, $i, 'mobileid');
                                                $_SESSION['mobileid'] = $mbid;
                                            }
                                            echo ">$name</option>";
                                            echo "</option>";
                                        }
                                        echo "</select>";
                                        pg_free_result($result);
                                        ?>
                                    </td></tr>
                                <tr><td colspan="2">
                                        <input style="padding:2px 4px 2px 4px; width:auto;" type="submit" name="submit" value="Gửi yêu cầu"/>
                                    </td></tr>
                            </table>
                        </form>
                    </div>
                    <div>
                        <?php
                        if (isset($_GET['model']))
                            $model = $_GET['model'];
                        else
                            $model = null;

                        if ($model != null) {
                            $query = "select * from mobilephone where model = '$model'";
                            $result = pg_query($connection, $query);
                            if (pg_num_rows($result) == 0)
                                echo("Model này không tồn tại");
                            else {
                                ?>
                                <form action="#" method="post">
                                    <table>
                                        <tr>
                                            <td colspan="2">Thông tin điện thoại</td></tr>
                                        <tr><td>Model</td><td>
                                                <input type="text" name="model" value="<?php echo pg_fetch_result($result, 0, 'model'); ?>"/>
                                            </td></tr>
                                        <tr><td>Giá</td><td>
                                                <input type="number" name="price" value="<?php echo pg_fetch_result($result, 0, 'price'); ?>"/>
                                            </td></tr>
                                        <tr><td>Số lượng trong kho</td><td>
                                                <input type="number" name="quantity" value="<?php echo pg_fetch_result($result, 0, 'quantity'); ?>" />
                                            </td></tr>
                                        <tr><td>Khuyến mại</td><td>
                                                <input type="text" name="promotions" value="<?php echo pg_fetch_result($result, 0, 'promotions'); ?>" />
                                            </td></tr>
                                        <tr><td>Bảo hành</td><td>
                                                <input type="text" name="warranty" value="<?php echo pg_fetch_result($result, 0, 'warranty'); ?>"/>
                                            </td></tr>
                                        <tr><td>Hệ điều hành</td><td>
                                                <input type="text" name="os" value="<?php echo pg_fetch_result($result, 0, 'os'); ?>" />
                                            </td></tr>
                                        <tr><td>Giải trí</td><td>
                                                <input type="text" name="entertainment" value="<?php echo pg_fetch_result($result, 0, 'entertainment'); ?>" />
                                            </td></tr>
                                        <tr><td>Kết nối mạng</td><td>
                                                <input type="text" name="network" value="<?php echo pg_fetch_result($result, 0, 'network'); ?>"/>
                                            </td></tr>
                                        <tr><td>Màn hình</td><td>
                                                <input type="text" name="screen" value="<?php echo pg_fetch_result($result, 0, 'screen'); ?>"/>
                                            </td></tr>
                                        <tr><td>Pin</td><td>
                                                <input type="text" name="battery" value="<?php echo pg_fetch_result($result, 0, 'battery'); ?>"/>
                                            </td></tr>
                                        <tr><td>Kiểu dáng dáng</td><td>
                                                <input type="text" name="appearance" value="<?php echo pg_fetch_result($result, 0, 'appearance'); ?>" />
                                            </td></tr>
                                    </table>
                                    <input style="padding:2px 4px 2px 4px; width:auto;" type="submit" value="Sửa thông tin" name="confirm" />
                                    <hr/>
                                </form>
                                <?php
                            }
                        }
                        if (isset($_REQUEST['confirm'])) {
                            $mobileid = $_SESSION['mobileid'];
                            $model = $_POST['model'];
                            $price = $_POST['price'];
                            $quantity = $_POST['quantity'];
                            $promotions = $_POST['promotions'];
                            $warranty = $_POST['warranty'];
                            $os = $_POST['os'];
                            $entertainment = $_POST['entertainment'];
                            $network = $_POST['network'];
                            $screen = $_POST['screen'];
                            $battery = $_POST['battery'];
                            $appearance = $_POST['appearance'];
                            $query = "update mobilephone set model = '$model', price = '$price', quantity = '$quantity', promotions = '$promotions'," .
                                    "warranty = '$warranty', os = '$os', entertainment = '$entertainment', network = '$network', screen = '$screen'," .
                                    "battery = '$battery', appearance = '$appearance' where mobileid = '$mobileid'";
                            $result = pg_query($connection, $query);
                            if (pg_affected_rows($result))
                                echo "Thay đổi thông tin của điện thoại thành công";
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
        <div id="footer">
            <?php include('modules/footer.php'); ?>
        </div>
    </body>
</html>
